<?php

   /**
     *    XML test displayer. Generates only
     *    failure messages and a pass count.
	 *	  @package SimpleTest
	 *	  @subpackage UnitTester
     */
    class XmlReporter extends SimpleReporter {
        var $_character_set;
        
        /**
         *    Does nothing yet. The first output will
         *    be sent on the first test start. For use
         *    by a web browser.
         *    @access public
         */
        function XmlReporter($character_set = 'ISO-8859-1') {
            $this->SimpleReporter();
            $this->_character_set = $character_set;
        }
        
        /**
         *    Paints the top of the web page setting the
         *    title to the name of the starting test.
         *    @param string $test_name      Name class of test.
         *    @access public
         */
        function paintHeader($test_name) {
            $this->sendNoCacheHeaders();
            header('Content-type: text/xml');
            print "<?xml version=\"1.0\"?>\n";
            print "<testcase>\n";
            flush();
        }
        
        /**
         *    Send the headers necessary to ensure the page is
         *    reloaded on every request. Otherwise you could be
         *    scratching your head over out of date test data.
         *    @access public
         *    @static
         */
        function sendNoCacheHeaders() {
            if (! headers_sent()) {
                header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
                header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
                header("Cache-Control: no-store, no-cache, must-revalidate");
                header("Cache-Control: post-check=0, pre-check=0", false);
                header("Pragma: no-cache");
            }
        }
        
        /**
         *    Paints the end of the test with a summary of
         *    the passes and failures.
         *    @param string $test_name        Name class of test.
         *    @access public
         */
        function paintFooter($test_name) {
            print "<passes>" . $this->getPassCount() . "</passes>\n";
            print "</testcase>\n";
        }
        
        /**
         *    Paints the test failure with a breadcrumbs
         *    trail of the nesting test suites below the
         *    top level test.
         *    @param string $message    Failure message displayed in
         *                              the context of the other tests.
         *    @access public
         */
        function paintFail($message) {
            parent::paintFail($message);
            print "<msg type='Fail'>\n";
          	print "<location>";

            $breadcrumb = $this->getTestList();
            array_shift($breadcrumb);

            echo implode(" -&gt; ", $breadcrumb);
          	print "</location>\n";

          	print "<output>";
            print $this->_htmlEntities($message);
          	print "</output>\n";
			print "</msg>\n";

/*	        parent::paintFail($message);
	        print "<span class=\"fail\">Fail</span>: ";
	    	$testOutputFormatter = new TestOutputFormatter();  	
	    	$lineNo = $testOutputFormatter->getLineNo($message);
	    	$filename = $testOutputFormatter->getFilename($message);

	    	print $lineNo . " - " . $filename . "<br>";
	        print "->$message<br />";
	        print "<br /><br />";
*/
        }
        
        /**
         *    Paints a PHP error or exception.
         *    @param string $message        Message is ignored.
         *    @access public
         *    @abstract
         */
        function paintException($message) {
            parent::paintException($message);
            print '<msg type="Exception">';
            $breadcrumb = $this->getTestList();
            array_shift($breadcrumb);
            print implode(" -&gt; ", $breadcrumb);
            print " -&gt; ";
            print $this->_htmlEntities($message) . "</msg>\n";
        }
        
        /**
         *    Paints formatted text such as dumped variables.
         *    @param string $message        Text to show.
         *    @access public
         */
        function paintFormattedMessage($message) {
                      print '<msg type="Message">';
            $breadcrumb = $this->getTestList();
            array_shift($breadcrumb);
            print implode(" -&gt; ", $breadcrumb);
            print " -&gt; ";
            print $this->_htmlEntities($message) . "</msg>\n";
            flush();
        }
        
        /**
         *    Character set adjusted entity conversion.
         *    @param string $message    Plain text or Unicode message.
         *    @return string            Browser readable message.
         *    @access protected
         */
        function _htmlEntities($message) {
            return htmlentities($message, ENT_COMPAT, $this->_character_set);
        }
    }
?>